package com.yjd.bi.fund.service.impl;

import com.yjd.bi.base.service.impl.BaseServiceImpl;
import com.yjd.bi.fund.dao.IBiFundsStatisticsDao;
import com.yjd.bi.fund.model.BiFundsStatisticsModel;
import com.yjd.bi.fund.service.IBiFundsStatisticsService;
import com.yjd.bi.fund.service.IPfBalanceService;
import com.yjd.bi.report.enums.TimeType;
import com.yjd.bi.report.model.EChartsModel;
import com.yjd.bi.report.util.TimeDimensionUtil;
import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.util.ArithUtil;
import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.NumberUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <b>description</b>：资金量记录业务实现<br>
 * <b>time</b>：2016-04-14 15:09:59 <br>
 * <b>author</b>：  chenpengfei@yijeidai.com
 */
@Service("biFundsStatisticsService")
public class BiFundsStatisticsServiceImpl extends BaseServiceImpl implements IBiFundsStatisticsService {

	@Resource
	private IBiFundsStatisticsDao biFundsStatisticsDao;
	@Resource
	private IPfBalanceService pfBalanceService;

	/**
	 * 插入
	 *
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public BiFundsStatisticsModel insert(BiFundsStatisticsModel model) throws Exception {
		return this.biFundsStatisticsDao.insertModel(model, true);
	}

	/**
	 * 更新
	 *
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public int update(BiFundsStatisticsModel model) throws Exception {
		return this.biFundsStatisticsDao.updateModel(model, true);
	}

	/**
	 * 根据id删除
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int delete(long id) throws Exception {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("id", id);
		return this.biFundsStatisticsDao.deleteModel(paramMap, true);
	}

	@Override
	public IBaseDao getBaseDao() {
		return this.biFundsStatisticsDao;
	}

	@Override
	public EChartsModel getDates(TimeType timeType, Long timeFrom,String field) throws Exception {
		// TODO Auto-generated method stub
		EChartsModel ec=new EChartsModel();
		ec.setxAxis(FrameUtil.newArrayList(null));
		ec.setLegend(FrameUtil.newArrayList(getFormateX(timeFrom)));
		ec.addSeries(FrameUtil.newArrayList(null), getFormateX(timeFrom));
		List<List<Long>> searchParam=TimeDimensionUtil.getSearchRanges(timeType,timeFrom);
		for(int i=searchParam.size()-1;i>=0;i--){
			ec.getXAxis().add(DateUtil.getFormateStringByTimeDimention(searchParam.get(i).get(1), "yyyy-MM-dd"));
			Map dataMap = FrameUtil.newHashMap("field", field, "start", searchParam.get(i).get(0), "end", searchParam.get(i).get(1));
			Double sum = 0d;
			if ("available".equals(field)) {
				sum = this.pfBalanceService.getfundsSumAvailable(searchParam.get(i).get(1));
			} else if ("all_available".equals(field)) {
				sum = this.pfBalanceService.getfundsSumAllAvailable(searchParam.get(i).get(1));
			} else {
				sum = this.biFundsStatisticsDao.getFundsSum(dataMap, false);
			}
			ec.getSeries().get(0).getData().add(NumberUtil.formatNumber(ArithUtil.round(sum, 2), "##0.00"));
		}
		return ec;
	}
	
	public String getFormateX(Long value){
		return DateUtil.timestampToDateString(DateUtil.dateStringToTimestamp(value+"", DateUtil.PATTERN_yyyyMMdd),DateUtil.PATTERN_yyyy_MM_dd);
	}
	
	public BiFundsStatisticsModel getSummaryModel(Long start,Long end) throws Exception{
		return this.biFundsStatisticsDao.getSummaryModel(FrameUtil.newHashMap("start",start,"end",end), true);
		
	}

	@Override
	public Double getLckSummary(Long start, Long end) throws Exception {
		// TODO Auto-generated method stub
		return this.biFundsStatisticsDao.getLckSummary(FrameUtil.newHashMap("start",start,"end",end), true);
	}
	
}
